var alert = require('../../helpers/alert.js')
var request = require('../../helpers/request.js')
var config = require('../../config.js')
const R = require('../../helpers/request.js')

// pages/zhuajiu/zhuajiu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    nickname: '',

    roomNumber: null,
    roomPeopleNum: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var me = this
    var userInfo = wx.getStorageSync('user')
    me.setData({
      nickname: userInfo.nickName,
      avatar: userInfo.avatarUrl
    })
    //TODO: 如果不存在就退回到主页
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  createRoom() {
    var me = this

    //Validate
    if (!me.data.roomPeopleNum) {
      alert.showWarn('必须输入房间人数')
      return
    }
    if (me.data.roomPeopleNum < 2) {
      alert.showWarn('房间最小人数为2')
      return
    }
    if (me.data.roomPeopleNum > 10) {
      alert.showWarn('房间最大人数为10')
      return
    }

    console.log('创建房间，人数: ' + me.data.roomPeopleNum)

    R.auth_request({
      url: config.service.zhuajiu + '/createRoom',
      data: { peopleNum: me.data.roomPeopleNum },
      success: data => {//Success
        console.log(data)

        if (data.ok == false) {
          alert.showWarn('创建房间错误: ' + data.msg)
          return
        }

        //Store the room info
        wx.setStorageSync('room', data.data)
        wx.navigateTo({
          url: '../zhuajiu_room/index',
        })
      },
      fail: err => {
        alert.showWarn('请求失败')
      }
    })

    // request.request(
    //   config.service.zhuajiu + '/createRoom',
    //   'POST',
    //   { peopleNum: me.data.roomPeopleNum },
    //   res => {//Success
    //     var data = res.data
    //     console.log(data)

    //     if (data.result != 'success'){
    //       alert.showWarn(data.msg)
    //       return
    //     }
          

    //     //Store the room info
    //     wx.setStorageSync('room', data.data)
    //     wx.navigateTo({
    //       url: '../zhuajiu_room/index',
    //     })
    //   },
    //   err => {
    //     alert.showWarn('请求失败')
    //   }
    // )
  },

  enterRoom() {
    var me = this
    if (!me.data.roomNumber) {
      alert.showWarn('必须输入房间号码')
      return
    }
    if (me.data.roomNumber < 0) {
      alert.showWarn('请输入正确的房号')
      return
    }

    console.log('进入房间，房号: ' + me.data.roomNumber)

    request.request(
      config.service.zhuajiu + '/enterRoom',
      'POST',
      { roomNum: me.data.roomNumber },
      res => {
        var data = res.data
        console.log(data)

        if (data.result != 'success'){
          alert.showWarn(data.msg)
          return
        }

        //Store the room info
        wx.setStorageSync('room', data.data)
        wx.navigateTo({
          url: '../zhuajiu_room/index',
        })
      },
      err => {
        alert.showWarn('请求失败')
      }
    )

  },

  bindRoomPeopleNum(e) {
    this.setData({
      roomPeopleNum: e.detail.value
    })
  },

  bindRoomNumber(e) {
    this.setData({
      roomNumber: e.detail.value
    })
  }
})